package servlets;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import careerSession.CareerSession;
import descriptor.CompaniesListDescriptor;
import descriptor.CompanyDescriptor;
import descriptor.CompanySupDescriptor;
import descriptor.CompanySupsListDescriptor;
import descriptor.DegreeListDescriptor;
import descriptor.FeedbackListDescriptor;
import descriptor.OfferDescriptor;
import descriptor.OfferListDescriptor;
import descriptor.RecruiterDescriptor;
import descriptor.RecruitersListDescriptor;
import descriptor.StudentListDescriptor;
import extras.Md5Encryption;

public class CareerServlet extends HttpServlet 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CareerSession careerSession = null;
	RequestDispatcher dispatcher = null ;
	private String action = "";
	
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
	{
		System.out.println("get");
		HttpSession session = req.getSession(true);
		careerSession = (CareerSession)session.getAttribute("careerSession");
		if(careerSession != null)
		{
			if(careerSession.isLogged())
			{
				action = (String) req.getAttribute("action");
				System.out.println("action = " +(String) req.getParameter("action"));
				if (action == null)
				{
					req.getSession().setAttribute("login",careerSession.getLogin());
					req.getSession().setAttribute("userSurname",careerSession.getSurName());
					req.getSession().setAttribute("userFirstname",careerSession.getFirstname());
					req.getSession().setAttribute("mail",careerSession.getMail());
					CompaniesListDescriptor compList = careerSession.getAllCompanies();
					req.setAttribute("Companies",compList);
					if(compList.sizeCompaniesList() > 0)
					{
						dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
					}
					else
					{
						req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
						dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
					}
				}
			}
		}
		else
		{
			req.setAttribute("error1", Notification.getNotification(Notification.appl_identification,""));
			dispatcher = req.getRequestDispatcher("index.jsp");
		}
		if (dispatcher != null) 
			dispatcher.forward(req, res) ;
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
	{
		System.out.println("post");
		HttpSession session = req.getSession(true);
		session.setMaxInactiveInterval(120);
		careerSession = (CareerSession)session.getAttribute("careerSession");
		if(careerSession != null)
		{
			if(careerSession.isLogged())
			{
				req.getSession().setAttribute("login",careerSession.getLogin());
				req.getSession().setAttribute("userSurname",careerSession.getSurName());
				req.getSession().setAttribute("userFirstname",careerSession.getFirstname());
				req.getSession().setAttribute("mail",careerSession.getMail());
				action = req.getParameter("action");
				System.out.println("action = " +(String) req.getParameter("action"));
				if (action == null)
				{
					doGet(req,res);
				}
				else
				{
					if(action.equals("viewInfo"))
					{
						dispatcher = req.getRequestDispatcher("Career/CareerInformation.jsp");
					}
					if(action.equals("settings"))
					{
						dispatcher = req.getRequestDispatcher("Career/CareerSettings.jsp");
					}
					if (action.equals("logout"))
					{
						req.setAttribute("error1", Notification.getNotification(Notification.logout,careerSession.getLogin()));
						session.invalidate();
						dispatcher = req.getRequestDispatcher("index.jsp");
					}
					if (action.equals("changeMail"))
					{
						String mail = req.getParameter("email");
						if(!mail.equals("") && mail.matches("(.+?)@(.+?)\\.(.+?)"))
						{
							careerSession.updateMail(mail);
							String msg = Notification.getNotification(Notification.mail_career_ok, careerSession.getLogin());
							req.setAttribute("msg", msg);
							req.getSession().setAttribute("mail",careerSession.getMail());
							dispatcher = req.getRequestDispatcher("Student/StudentSettings.jsp");
						}
						else
						{
							req.setAttribute("error1",Notification.getNotification(Notification.mail_career_exp, ""));
							req.setAttribute("error2",mail);
							dispatcher = req.getRequestDispatcher("Career/CareerSettings.jsp");
						}
						
					}
					if (action.equals("changePasswd"))
					{
						String pass1 = req.getParameter("Oldpasswd");
						String pass2 = req.getParameter("Newpasswd1");
						String pass3 = req.getParameter("Newpasswd2");
						try 
						{
							if(Md5Encryption.testPassword(pass1, careerSession.getPassword()))
							{
								if(pass2.equals(pass3))
								{	
									if(pass2.length()>=5)
									{
										careerSession.updatePasswd(Md5Encryption.getEncodedPassword(pass2));
										String msg2 = Notification.getNotification(Notification.password_career_ok, careerSession.getLogin());
										req.setAttribute("msg2", msg2);
										dispatcher = req.getRequestDispatcher("Career/CareerSettings.jsp");
									}
									else
									{
										req.setAttribute("error3",Notification.getNotification(Notification.password_student_length, ""));
										dispatcher = req.getRequestDispatcher("Career/CareerSettings.jsp");
									}	
									
								}
								else
								{
									req.setAttribute("error3",Notification.getNotification(Notification.password_2_error, ""));
									dispatcher = req.getRequestDispatcher("Career/CareerSettings.jsp");
								}
							}
							else
							{
								req.setAttribute("error3",Notification.getNotification(Notification.password_wrong, careerSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Career/CareerSettings.jsp");
							}
						} 
						catch (NoSuchAlgorithmException e) 
						{
							req.setAttribute("error3",Notification.getNotification(Notification.password_wrong, "CareerServlet"));
							dispatcher = req.getRequestDispatcher("Career/CareerSettings.jsp");
						}
					}
					if (action.equals("viewCompanies"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("viewComp1"))
					{
						String id = req.getParameter("companyID");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(!(id.equals("")))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								int compID = Integer.parseInt(id);
								CompanyDescriptor compDes = careerSession.getCompany(compID);
								if(compDes != null)
								{
									req.setAttribute("companyID",id);
									req.setAttribute("CompanyDes",compDes);
									req.setAttribute("viewComp", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.company_notFound,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
					}
					if (action.equals("addCompany"))
					{
						dispatcher = req.getRequestDispatcher("Career/CareerCompanyAdd.jsp");
					}
					if (action.equals("addedCompany"))
					{
						String name = req.getParameter("CompName");
						String address = req.getParameter("CompAddr");
						String phone = req.getParameter("CompPhone");
						String email = req.getParameter("CompEmail");
						String cityName = req.getParameter("CompCity");
						String countryName = req.getParameter("CompCountry");
						String sectorName = req.getParameter("CompSector");
						int compID = careerSession.createCompany(name, address, phone, email, cityName, countryName, sectorName);
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compID != -1)
						{
							if(compList.sizeCompaniesList() > 0)
							{
								CompanyDescriptor compDes = careerSession.getCompany(compID);
								if(compDes != null)
								{
									req.setAttribute("companyID",Integer.toString(compID));
									req.setAttribute("CompanyDes",compDes);
									req.setAttribute("viewComp", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.company_notFound,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_createCompany_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanyAdd.jsp");
						}
					}
					if (action.equals("updateCompany"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerCompanyUpdate.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("viewComp2"))
					{
						String id = req.getParameter("companyID");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(!(id.equals("")))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								int compID = Integer.parseInt(id);
								CompanyDescriptor compDes = careerSession.getCompany(compID);
								if(compDes != null)
								{
									req.setAttribute("companyID",id);
									req.setAttribute("CompanyDes",compDes);
									req.setAttribute("viewComp", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerCompanyUpdate.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.company_notFound,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
					}
					if (action.equals("updatedCompany"))
					{
						int compID = Integer.parseInt(req.getParameter("compID"));
						String name = req.getParameter("CompName");
						String address = req.getParameter("CompAddr");
						String phone = req.getParameter("CompPhone");
						String email = req.getParameter("CompEmail");
						String cityName = req.getParameter("CompCity");
						String countryName = req.getParameter("CompCountry");
						String sectorName = req.getParameter("CompSector");
						if(careerSession.updateCompany(compID, name, address, phone, email, cityName, countryName, sectorName))
						{
							CompaniesListDescriptor compList = careerSession.getAllCompanies();
							req.setAttribute("Companies",compList);
							if(compList.sizeCompaniesList() > 0)
							{
								CompanyDescriptor compDes = careerSession.getCompany(compID);
								if(compDes != null)
								{
									req.setAttribute("companyID",Integer.toString(compID));
									req.setAttribute("CompanyDes",compDes);
									req.setAttribute("viewComp", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.company_notFound,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_updateCompany_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanyAdd.jsp");
						}
					}
					if(action.equals("viewRecruiters"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("viewRecruiter1"))
					{
						String id = req.getParameter("companyID");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(!id.equals(""))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								int compID = Integer.parseInt(id);
								RecruitersListDescriptor recruiterDes = careerSession.getAllRecruiters(compID);
								if(recruiterDes.sizeRecruitersList() > 0)
								{
									req.setAttribute("companyID",id);
									req.setAttribute("RecruiterDes",recruiterDes);
									req.setAttribute("viewRecruiters", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_recruiter,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
					}
					if (action.equals("addRecruiter"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerRecruiterAdd.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("addedRecruiter"))
					{
						int compID = Integer.parseInt(req.getParameter("compID"));
						String firstName = req.getParameter("CompRecFName");
						String name = req.getParameter("CompRecSName");
						String email = req.getParameter("CompRecEmail");
						String passwd = "";
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compID != -1)
						{
							int recruiterID = careerSession.createRecruiter(firstName, name, email, passwd, email, compID);
							if(recruiterID != -1)
							{
								
								if(compList.sizeCompaniesList() > 0)
								{
									RecruitersListDescriptor recruiterDes = careerSession.getAllRecruiters(compID);
									if(recruiterDes.sizeRecruitersList() > 0)
									{
										req.setAttribute("companyID",Integer.toString(compID));
										req.setAttribute("RecruiterDes",recruiterDes);
										req.setAttribute("viewRecruiters", "1");
										dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
									}
									else
									{
										req.setAttribute("error2", Notification.getNotification(Notification.career_no_recruiter,careerSession.getLogin()));
										dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
									}
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.career_createRecruiter_error,careerSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Career/CareerRecruiterAdd.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_no_comSelect,""));
							dispatcher = req.getRequestDispatcher("Career/CareerRecruiterAdd.jsp");
						}
					}
					if (action.equals("updateRecruiter"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						int compID = Integer.parseInt(req.getParameter("recruiterCompID"));
						int recruiterID = Integer.parseInt(req.getParameter("recruiterID"));
						RecruiterDescriptor recruiterDes = careerSession.getRecruiter(recruiterID);
						req.setAttribute("recruiterDes",recruiterDes);
						CompanyDescriptor compDes = careerSession.getCompany(compID);
						req.setAttribute("compDes",compDes);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerRecruiterUpdate.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("updatedRecruiter"))
					{
						int compID = Integer.parseInt(req.getParameter("compID"));
						int recruiterID = Integer.parseInt(req.getParameter("recruiterID"));
						String firstname = req.getParameter("CompRecFName");
						String name = req.getParameter("CompRecSName");
						String email = req.getParameter("CompRecEmail");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(careerSession.updateRecruiter(recruiterID, firstname, name, email, email, compID))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								RecruitersListDescriptor recruiterDes = careerSession.getAllRecruiters(compID);
								if(recruiterDes.sizeRecruitersList() > 0)
								{
									req.setAttribute("companyID",Integer.toString(compID));
									req.setAttribute("RecruiterDes",recruiterDes);
									req.setAttribute("viewRecruiters", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_recruiter,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_updateRecruiter_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
						}
					}
					if (action.equals("deleteRecruiter"))
					{
						int compID = Integer.parseInt(req.getParameter("recruiterCompID"));
						int recruiterID = Integer.parseInt(req.getParameter("recruiterID"));
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(careerSession.deleteRecruiter(recruiterID))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								RecruitersListDescriptor recruiterDes = careerSession.getAllRecruiters(compID);
								if(recruiterDes.sizeRecruitersList() > 0)
								{
									req.setAttribute("companyID",Integer.toString(compID));
									req.setAttribute("RecruiterDes",recruiterDes);
									req.setAttribute("viewRecruiters", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_recruiter,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerRecruiters.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_deleteRecruiter_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerRecruiterAdd.jsp");
						}
					}
					if(action.equals("viewSupervisors"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("viewSupervisor1"))
					{
						String id = req.getParameter("companyID");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(!id.equals(""))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								int compID = Integer.parseInt(id);
								CompanySupsListDescriptor supervisorDes = careerSession.getAllSupervisors(compID);
								if(supervisorDes.getCompanySupDescriptors().size() > 0)
								{
									req.setAttribute("companyID",id);
									req.setAttribute("SupervisorDes",supervisorDes);
									req.setAttribute("viewSupervisors", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_supervisor,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
					}
					if (action.equals("addSupervisor"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerSupervisorAdd.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("addedSupervisor"))
					{
						int compID = Integer.parseInt(req.getParameter("compID"));
						String firstName = req.getParameter("CompSupFName");
						String name = req.getParameter("CompSupSName");
						String email = req.getParameter("CompSupEmail");
						String passwd = "";
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compID != -1)
						{
							int supervisorID = careerSession.createSupervisor(firstName, name, email, passwd, email, compID);
							if(supervisorID != -1)
							{
								if(compList.sizeCompaniesList() > 0)
								{
									CompanySupsListDescriptor supervisorDes = careerSession.getAllSupervisors(compID);
									if(supervisorDes.getCompanySupDescriptors().size() > 0)
									{
										req.setAttribute("companyID",Integer.toString(compID));
										req.setAttribute("SupervisorDes",supervisorDes);
										req.setAttribute("viewSupervisors", "1");
										dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
									}
									else
									{
										req.setAttribute("error2", Notification.getNotification(Notification.career_no_supervisor,careerSession.getLogin()));
										dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
									}
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.career_createSupervisor_error,careerSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Career/CareerSupervisorAdd.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_no_comSelect,""));
							dispatcher = req.getRequestDispatcher("Career/CareerSupervisorAdd.jsp");
						}
					}
					if (action.equals("updateSupervisor"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						int compID = Integer.parseInt(req.getParameter("supervisorCompID"));
						int supervisorID = Integer.parseInt(req.getParameter("supervisorID"));
						CompanySupDescriptor supervisorDes = careerSession.getSupervisor(supervisorID);
						req.setAttribute("supervisorDes",supervisorDes);
						CompanyDescriptor compDes = careerSession.getCompany(compID);
						req.setAttribute("compDes",compDes);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerSupervisorUpdate.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("updatedSupervisor"))
					{
						int compID = Integer.parseInt(req.getParameter("compID"));
						int supervisorID = Integer.parseInt(req.getParameter("supervisorID"));
						String firstname = req.getParameter("CompSupFName");
						String name = req.getParameter("CompSupSName");
						String email = req.getParameter("CompSupEmail");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(careerSession.updateSupervisor(supervisorID, firstname, name, email, email, compID))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								CompanySupsListDescriptor supervisorDes = careerSession.getAllSupervisors(compID);
								if(supervisorDes.getCompanySupDescriptors().size() > 0)
								{
									req.setAttribute("companyID",Integer.toString(compID));
									req.setAttribute("SupervisorDes",supervisorDes);
									req.setAttribute("viewSupervisors", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_supervisor,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_updateSupervisor_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
						}
					}
					if (action.equals("deleteSupervisor"))
					{
						int compID = Integer.parseInt(req.getParameter("supervisorCompID"));
						int supervisorID = Integer.parseInt(req.getParameter("supervisorID"));
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(careerSession.deleteSupervisor(supervisorID))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								CompanySupsListDescriptor supervisorDes = careerSession.getAllSupervisors(compID);
								if(supervisorDes.getCompanySupDescriptors().size() > 0)
								{
									req.setAttribute("companyID",Integer.toString(compID));
									req.setAttribute("SupervisorDes",supervisorDes);
									req.setAttribute("viewSupervisors", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_supervisor,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerSupervisors.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_deleteSupervisor_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerSupervisorAdd.jsp");
						}
					}
					if(action.equals("viewOffers"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("viewOffer1"))
					{
						String id = req.getParameter("companyID");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(!(id.equals("")))
						{
							
							if(compList.sizeCompaniesList() > 0)
							{
								int compID = Integer.parseInt(id);
								OfferListDescriptor offerDes = careerSession.getAllOffers(compID);
								if(offerDes.sizeOfferList() > 0)
								{
									req.setAttribute("companyID",id);
									req.setAttribute("OfferDes",offerDes);
									req.setAttribute("viewOffers", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_offer,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
					}
					if (action.equals("addOffer"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerOfferAdd.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("addOffer1"))
					{
						String id = req.getParameter("companyID");
						req.setAttribute("companyID",id);
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(!id.equals(""))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								int compID = Integer.parseInt(id);
								RecruitersListDescriptor recruiterDes = careerSession.getAllRecruiters(compID);
								req.setAttribute("RecruiterDes",recruiterDes);
								CompanySupsListDescriptor supervisorDes = careerSession.getAllSupervisors(compID);
								req.setAttribute("SupervisorDes",supervisorDes);
								req.setAttribute("addOffers", "1");
								dispatcher = req.getRequestDispatcher("Career/CareerOfferAdd.jsp");
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
					}
					if (action.equals("addedOffer"))
					{
						int compID = Integer.parseInt(req.getParameter("companyID"));
						int recruiterID = Integer.parseInt(req.getParameter("recruiterID"));
						int supervisorID = Integer.parseInt(req.getParameter("supervisorID"));
						String name = req.getParameter("CompOfferName");
						String description = req.getParameter("CompOfferDescription");
						String dateOfStart = req.getParameter("calendar1");
						String dateOfEnd = req.getParameter("calendar2");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						req.setAttribute("companyID",Integer.toString(compID));
						if(compID != -1)
						{
							int offerID = careerSession.createOffer(name, description, dateOfStart, dateOfEnd, compID, recruiterID, supervisorID);
							
							if(offerID != -1)
							{
								
								if(compList.sizeCompaniesList() > 0)
								{
									OfferListDescriptor offerDes = careerSession.getAllOffers(compID);
									if(offerDes.sizeOfferList() > 0)
									{
										
										req.setAttribute("OfferDes",offerDes);
										req.setAttribute("viewOffers", "1");
										dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
									}
									else
									{
										req.setAttribute("error2", Notification.getNotification(Notification.career_no_offer,careerSession.getLogin()));
										dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
									}
								}
								else
								{
									req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
									dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.career_createOffer_error,careerSession.getLogin()));
								dispatcher = req.getRequestDispatcher("Career/CareerOfferAdd.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_no_comSelect,""));
							dispatcher = req.getRequestDispatcher("Career/CareerOfferAdd.jsp");
						}
					}
					if (action.equals("updateOffer"))
					{
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						int compID = Integer.parseInt(req.getParameter("offerCompID"));
						int offerID = Integer.parseInt(req.getParameter("offerID"));
						OfferDescriptor offerDes = careerSession.getOffer(offerID);
						req.setAttribute("offerDes",offerDes);
						CompanyDescriptor compDes = careerSession.getCompany(compID);
						req.setAttribute("compDes",compDes);
						RecruitersListDescriptor recruiterDes = careerSession.getAllRecruiters(compID);
						req.setAttribute("RecruiterDes",recruiterDes);
						CompanySupsListDescriptor supervisorDes = careerSession.getAllSupervisors(compID);
						req.setAttribute("SupervisorDes",supervisorDes);
						if(compList.sizeCompaniesList() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerOfferUpdate.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
							dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
						}
					}
					if (action.equals("updatedOffer"))
					{
						int compID = Integer.parseInt(req.getParameter("compID"));
						int offerID = Integer.parseInt(req.getParameter("offerID"));
						int recruiterID = Integer.parseInt(req.getParameter("recruiterID"));
						int supervisorID = Integer.parseInt(req.getParameter("supervisorID"));
						String name = req.getParameter("CompOfferName");
						String description = req.getParameter("CompOfferDescription");
						String dateOfStart = req.getParameter("calendar1");
						String dateOfEnd = req.getParameter("calendar2");
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(careerSession.updateOffer(offerID, name, description, dateOfStart, dateOfEnd, compID, recruiterID, supervisorID))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								OfferListDescriptor offerDes = careerSession.getAllOffers(compID);
								if(offerDes.sizeOfferList() > 0)
								{
									req.setAttribute("companyID", Integer.toString(compID));
									req.setAttribute("OfferDes",offerDes);
									req.setAttribute("viewOffers", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_offer,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_updateOffer_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
						}
					}
					if (action.equals("deleteOffer"))
					{
						int compID = Integer.parseInt(req.getParameter("offerCompID"));
						int offerID = Integer.parseInt(req.getParameter("offerID"));
						CompaniesListDescriptor compList = careerSession.getAllCompanies();
						req.setAttribute("Companies",compList);
						if(careerSession.deleteOffer(offerID))
						{
							if(compList.sizeCompaniesList() > 0)
							{
								OfferListDescriptor offerDes = careerSession.getAllOffers(compID);
								if(offerDes.sizeOfferList() > 0)
								{
									req.setAttribute("companyID", Integer.toString(compID));
									req.setAttribute("OfferDes",offerDes);
									req.setAttribute("viewOffers", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_offer,careerSession.getLogin()));
									dispatcher = req.getRequestDispatcher("Career/CareerOffers.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.no_company,""));
								dispatcher = req.getRequestDispatcher("Career/CareerCompanies.jsp");
							}
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_deleteOffer_error,careerSession.getLogin()));
							dispatcher = req.getRequestDispatcher("Career/CareerOfferAdd.jsp");
						}
					}
					if (action.equals("feedbackStudent"))
					{
						StudentListDescriptor studentList = careerSession.getAllStudents();
						req.setAttribute("Students",studentList);
						if(studentList.getStudentList().size() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerFeedBackStudent.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_noStudent,""));
							dispatcher = req.getRequestDispatcher("Career/CareerFeedBackStudent.jsp");
						}
					}
					if (action.equals("feedbackDegree"))
					{
						DegreeListDescriptor degreeList = careerSession.getAllDegrees();
						req.setAttribute("Degrees",degreeList);
						if(degreeList.getDegreeList().size() > 0)
						{
							dispatcher = req.getRequestDispatcher("Career/CareerFeedBackDegree.jsp");
						}
						else
						{
							req.setAttribute("error1", Notification.getNotification(Notification.career_noDegree,""));
							dispatcher = req.getRequestDispatcher("Career/CareerFeedBackDegree.jsp");
						}
					}
					if (action.equals("viewFeedbacks1"))
					{
						String id = req.getParameter("stdtID");
						StudentListDescriptor studentList = careerSession.getAllStudents();
						req.setAttribute("Students",studentList);
						if(!(id.equals("")))
						{
							req.setAttribute("studentID",id);
							if(studentList.getStudentList().size() > 0)
							{
								int studentID = Integer.parseInt(id);
								FeedbackListDescriptor feedbackDes = careerSession.getAllFeedbackStudent(studentID);
								if(feedbackDes.getFeedbackDescriptors().size() > 0)
								{
									req.setAttribute("FeedbackDes",feedbackDes);
									req.setAttribute("viewFeedbacks", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerFeedBackStudent.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_studentFeedback,""));
									dispatcher = req.getRequestDispatcher("Career/CareerFeedBackStudent.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.career_noStudent,""));
								dispatcher = req.getRequestDispatcher("Career/CareerFeedBackStudent.jsp");
							}
						}
					}
					if (action.equals("viewFeedbacks2"))
					{
						String id = req.getParameter("degID");
						DegreeListDescriptor degreeList = careerSession.getAllDegrees();
						req.setAttribute("Degrees",degreeList);
						if(!(id.equals("")))
						{
							
							if(degreeList.getDegreeList().size() > 0)
							{
								int degreeID = Integer.parseInt(id);
								FeedbackListDescriptor feedbackDes = careerSession.getAllFeedbackDegree(degreeID);
								if(feedbackDes.getFeedbackDescriptors().size() > 0)
								{
									req.setAttribute("degreeID",id);
									req.setAttribute("FeedbackDes",feedbackDes);
									req.setAttribute("viewFeedbacks", "1");
									dispatcher = req.getRequestDispatcher("Career/CareerFeedBackDegree.jsp");
								}
								else
								{
									req.setAttribute("error2", Notification.getNotification(Notification.career_no_degreeFeedback,""));
									dispatcher = req.getRequestDispatcher("Career/CareerFeedBackDegree.jsp");
								}
							}
							else
							{
								req.setAttribute("error1", Notification.getNotification(Notification.career_noDegree,""));
								dispatcher = req.getRequestDispatcher("Career/CareerFeedBackDegree.jsp");
							}
						}
					}
				}
			}
		}
		else
		{
			req.setAttribute("error1", Notification.getNotification(Notification.appl_identification,""));
			dispatcher = req.getRequestDispatcher("index.jsp");
		}
		if (dispatcher != null) 
			dispatcher.forward(req, res) ;
	}
}
